package com.zfast.config.controller;

import com.zfast.config.constant.SessionKeyConst;
import com.zfast.config.entity.Icon;
import com.zfast.config.entity.Menu;
import com.zfast.config.service.IconService;
import com.zfast.config.service.TreeMenuService;
import com.zfast.config.vo.CommonResult;
import com.zfast.config.vo.Data;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @Classname PermissionController
 * @Description None
 * @Date 2019/7/21 9:51
 * @Created by WDD
 */
@Controller
@RequestMapping("/manager")
public class PermissionController {

    @Resource
    private CommonResult commonResult;
    @Resource
    private TreeMenuService treeMenuService;
    @Resource
    private IconService iconService;


    /**
     * 跳转登录界面
     *
     * @return
     */
    @GetMapping("/permission")
    public String permission() {
        return "manager/permission/permissionList";
    }


    /**
     * 异步加载权限树
     *
     * @param session
     * @return
     */
    @GetMapping("/permissionList")
    @ResponseBody
    public Object permissionList(HttpSession session, String id) {
        List<Menu> menuList = treeMenuService.selectAll();
        if (id != null) {
            List<Menu> atList = treeMenuService.selectByRoleId(id);
            for (Menu info : menuList) {
                for (Menu at : atList) {
                    if (info.getId().equals(at.getId())) {
                        info.setChecked(true);
                    }
                }
            }
        }
        HashMap<String, Object> rest = new HashMap<>();
        rest.put("code", 0);
        rest.put("msg", "ok");
        rest.put("data", menuList);

        return rest;
    }

    /**
     * 跳转页面
     *
     * @param type
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/addPermission")
    public String addPermission(String type, String id, Model model, HttpSession session) {
        //加载icon列表,存入session
        if (session.getAttribute(SessionKeyConst.ICON) == null) {
            List<Icon> iconList = iconService.selectAll();
            session.setAttribute(SessionKeyConst.ICON, iconList);
        }
        if (type != null && type.equals("edit")) {
            Menu menu = treeMenuService.selectById(id);
            model.addAttribute(SessionKeyConst.TREEMENU, menu);
        } else if (type != null && type.equals("add")) {
            model.addAttribute("pid", id); //设为父id
        } else if (type != null && type.equals("addParent")) {
            return "manager/permission/addParentNode";
        }
        return "manager/permission/addPermission";
    }

    /**
     * 添加修改权限
     *
     * @param menu
     * @return
     */
    @PostMapping("/addPermission")
    @ResponseBody
    public CommonResult submitAddPermission(Menu menu) {
        Menu byName = treeMenuService.selectByName(menu.getName());
        Menu byUrl = treeMenuService.selectByUrl(menu.getUrl());

        if (menu.getId() != null) {
            //修改
            if (byName != null && !byName.getId().equals(menu.getId())) {
                commonResult.ajaxFalse("权限名已存在");
                return commonResult;
            }
            if (byUrl != null && !byUrl.getId().equals(menu.getId())) {
                commonResult.ajaxFalse("地址已存在");
                return commonResult;
            }
            int count = treeMenuService.editByPermission(menu);
            if (count > 0) {
                commonResult.ajaxTrue("修改成功");
            } else {
                commonResult.ajaxFalse("修改失败");
            }
        } else {
            //添加
            if (byName != null) {
                commonResult.ajaxFalse("权限名已存在");
                return commonResult;
            }
            if (byUrl != null) {
                commonResult.ajaxFalse("地址已存在");
                return commonResult;
            }
            //若是添加父节点
            if (menu.getPid().equals(-1)) {
                menu.setUrl("-1");
            }
            int count = treeMenuService.insertPermission(menu);
            if (count > 0) {
                commonResult.ajaxTrue("添加成功");
            } else {
                commonResult.ajaxFalse("添加失败");
            }
        }
        return commonResult;
    }

    /**
     * 删除权限
     *
     * @param data
     * @return
     */
    @PostMapping("/delPermission")
    @ResponseBody
    public CommonResult delPermission(Data data) {
        List<Menu> menuList = treeMenuService.selectByPid(data.getIds().get(0));
        if (menuList.size() != 0) {
            commonResult.ajaxFalse("请先删除子节点");
            return commonResult;
        }
        int count = treeMenuService.delByPermissionIds(data.getIds());
        if (count >= data.getIds().size()) {
            commonResult.ajaxTrue("删除成功");
        } else {
            commonResult.ajaxFalse("删除失败");
        }
        return commonResult;
    }

    /**
     * 给角色分配权限
     *
     * @param data
     * @param id
     * @return
     */
    @PostMapping("/allotPer")
    @ResponseBody
    public CommonResult allotPre(Data data, String id) {
        int count = treeMenuService.updateRolePermission(data.getIds(), id);
        if (count >= data.getIds().size()) {
            commonResult.ajaxTrue("分配成功");
        }
        return commonResult;
    }

}
